package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_enums;

import cn.com.api.service.ApiT_enumsService;
import cn.com.backstage.controller.base.T_enumsBaseController;

import cn.com.util.DateUtil;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_enumsController  extends T_enumsBaseController {

	private static Logger logger = Logger.getLogger(T_enumsController.class);
	
	@Autowired
	private ApiT_enumsService apiT_enumsService;
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "enums-list")
	public ModelAndView enums_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_enums_list.jsp");
		
		String id = request.getParameter("id");
		List<ApiT_enums> t_enumss = null;
		
		if(request.getParameter("id")!=null){
			t_enumss = apiT_enumsService.findT_enumssByParent_id(id);
		}else{
			t_enumss = apiT_enumsService.findT_enumss();
		}
		mv.addObject("t_enumss", t_enumss);
		//
		List<ApiT_enums> enumss = apiT_enumsService.findT_enumss();
		mv.addObject("enumss", enumss);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "enums-add-pre")
	public ModelAndView enums_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_enums_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_enums t_enums = apiT_enumsService.findT_enumsById(Integer.valueOf(id));
			mv.addObject("t_enums",t_enums);
		}
		//
		List<ApiT_enums> enumss = apiT_enumsService.findT_enumss();
		mv.addObject("enumss", enumss);
		//
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "enums-add")
	public void enums_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String parent_id = request.getParameter("parent_id");
		String sort = request.getParameter("sort");
		String item_key = request.getParameter("item_key");
		String display_name = request.getParameter("display_name");
		String item_value = request.getParameter("item_value");
		String create_time = request.getParameter("create_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_enums t_enums = new ApiT_enums();
        
		    //t_enums.setId(id);
		    t_enums.setParent_id(parent_id);
		    t_enums.setSort(Integer.valueOf(sort));
		    t_enums.setItem_key(item_key);
		    t_enums.setDisplay_name(display_name);
		    t_enums.setItem_value(item_value);
		    t_enums.setCreate_time(DateUtil.nowTimeToString());
	     
			apiT_enumsService.insertT_enums(t_enums);
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		}
		//edit
		else{
			ApiT_enums t_enums = apiT_enumsService.findT_enumsById(Integer.valueOf(id));
	      
		    t_enums.setParent_id(parent_id);
		    t_enums.setSort(Integer.valueOf(sort));
		    t_enums.setItem_key(item_key);
		    t_enums.setDisplay_name(display_name);
		    t_enums.setItem_value(item_value);
		    //t_enums.setCreate_time(create_time);
			
			apiT_enumsService.updateT_enums(t_enums);
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



